package com.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.configure.Result;
import com.hospital.entity.Medicine;
import com.hospital.mapper.MedicineMapper;
import com.hospital.service.MedicineService;
import com.hospital.utils.IDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author WuLiting
 * @since 2021-03-24
 */
@Service
public class MedicineServiceImpl extends ServiceImpl<MedicineMapper, Medicine> implements MedicineService {

    @Resource
    MedicineMapper medicineMapper;

    @Override
    public Result<Medicine> updateMedicine(Medicine medicine) {
        Result<Medicine> result = new Result<>();
        try {
            if (StringUtils.isEmpty(medicine.getMedicineId())) {
                //如果ID为空，代表添加
                medicine.setMedicineId(IDGenerator.getUUID("MEDICINE"));
                medicineMapper.insert(medicine);
            } else {
                //如果ID不为空，代表修改
                medicineMapper.updateById(medicine);
            }
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Medicine> delMedicine(Medicine medicine) {
        Result<Medicine> result = new Result<>();
        try {
            if (StringUtils.isEmpty(medicine.getMedicineId())) {
                result.setMessage("medicineId can't be empty");
                return result;
            }
            medicineMapper.deleteById(medicine.getMedicineId());
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Medicine> batchDelMedicine(Medicine medicine) {
        Result<Medicine> result = new Result<>();
        try {
            if (!StringUtils.isEmpty(medicine.getIds())) {
                String[] ids = medicine.getIds().split(",");
                for (String id : ids) {
                    medicineMapper.deleteById(id);
                }
            }
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Medicine> queryMedicine(Medicine medicine) {
        Result<Medicine> result = new Result<>();
        try {
            QueryWrapper<Medicine> wrapper = new QueryWrapper<>();
            //查询条件拼接，仅限单表查询可用
            if (!StringUtils.isEmpty(medicine.getMedicineName())) {
                wrapper.lambda().like(Medicine::getMedicineName, medicine.getMedicineName());
            }
            if (!StringUtils.isEmpty(medicine.getMedicineStatus())) {
                wrapper.lambda().like(Medicine::getMedicineStatus, medicine.getMedicineStatus());
            }

            List<Medicine> medicines = medicineMapper.selectList(wrapper);
            result.setReturnList(medicines);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Medicine> queryMedicineByPage(Medicine medicine) {
        Result<Medicine> result = new Result<>();

        try {
            if (medicine.getCurrent() == null || medicine.getSize() == null) {
                result.setMessage("current or size can't be null");
                return result;
            }

            QueryWrapper<Medicine> wrapper = new QueryWrapper<>();
            //查询条件拼接，仅限单表查询可用
            if (!StringUtils.isEmpty(medicine.getMedicineTypeId())) {
                wrapper.lambda().eq(Medicine::getMedicineTypeId, medicine.getMedicineTypeId());
            }
            if (!StringUtils.isEmpty(medicine.getMedicineName())) {
                wrapper.lambda().like(Medicine::getMedicineName, medicine.getMedicineName());
            }

            IPage<Medicine> medicines = medicineMapper.selectPage(new Page<>(medicine.getCurrent(), medicine.getSize()), wrapper);
            result.setReturnPage(medicines);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<Medicine> queryMedicineById(Medicine medicine) {
        Result<Medicine> result = new Result<>();
        try {
            Medicine returnMedicine = medicineMapper.selectById(medicine.getMedicineId());
            result.setReturnObj(returnMedicine);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

}
